//
//  LCCardTaskDetailViewController.m
//  lechuang
//
//  Created by Mr_hi on 2024/7/11.
//  Copyright © 2024 Mooyi. All rights reserved.
//

#import "LCCardTaskDetailViewController.h"
#import "LCTaskDetailInfoTableViewCell.h"
#import "LCTaskService.h"

#import "LCCardTaskApplyViewController.h"

@interface LCCardTaskDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic)LCCardTaskModel * detail;


@end

@implementation LCCardTaskDetailViewController


- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getTaskDetail];
}

- (void)getTaskDetail {
    [LCTaskService getHaoKaTaskDetailWithId:self.goodId Block:^(BOOL isSuccess, LCCardTaskModel *detail) {
        if (isSuccess) {
            self.detail = detail;
            [self.tableView reloadData];
        }
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0: {
            return 1;
        } break;
        default: {
            return self.detail.details.count;
        } break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0 : {
            NSString * text = [NSString stringWithFormat:@"结算要求：%@",self.detail.compliance];
            CGFloat height = [text getSpaceLabelHeightwithSpeace:3 withFont:[UIFont systemFontOfSize:13] withWidth:(ScreenW-28)];
            return 145 + height + 15;
        } break;
            
        default: {
            NSString * imgstr = self.detail.details[indexPath.row];
            UIImage * img = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgstr];
            if (!img) {
                img = [UIImage imageNamed:@"lc_share_bg"];
            }
            CGFloat height = img.size.height;
            return (height/img.size.width)*ScreenW;
        } break;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0: {
            LCCardDetailHeadTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCCardDetailHeadTableViewCell"];
            [cell setSysPoint:self.sysPoint Task:self.detail];
            return cell;
        } break;
            
        default: {
            LCCardDetailImageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCCardDetailImageTableViewCell"];
            [self confirmCell:cell atIndexPath:indexPath];
            return cell;
        } break;
    }
}

- (void)confirmCell:(LCCardDetailImageTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    NSString *imgUrl = self.detail.details[indexPath.row];
    UIImage *cachedImg = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgUrl];
    if (!cachedImg) {
        [self downloadImage:imgUrl forIndexPath:indexPath];
    }else{
        cell.detailImgView.image  = cachedImg;
    }
}

- (void)downloadImage:(NSString *)imageURL forIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageURL] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        [[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES completion:^{
            [weakSelf performSelectorOnMainThread:@selector(reloadCellAtIndexPath:) withObject:indexPath waitUntilDone:NO];
        }];
    }];
}

-(void)reloadCellAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

- (IBAction)applyBtnAction:(UIButton *)sender {
    LCCardTaskApplyViewController * pushVC = [LCCardTaskApplyViewController xx_instantiateFromStoryboardNamed:@"Task"];
    pushVC.point = self.sysPoint;
    pushVC.task = self.detail;
    [self.navigationController pushViewController:pushVC animated:YES];
}




@end
